<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Services\Format\JsonFormat;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de listing des auteurs
 *
 * @package Services
 */
class ListUsersService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param $request      the HTTP request to be treated by the action
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $term = $request->getParameter("term");

        $userDao = DAOFactory::getDAO("User");
        $users = $userDao->retrieveByPartialName($term);

        $objects = array();
        if (!is_null($users)) {
            foreach ($users as $user) {
                $objects[] = new JsonFormat($user->getName(), $user->getId());
            }
        }

        $this->_response->setAttribute("objects", $objects);
        $this->show($this->_format);
    }
}
